package com.project.logistics.controller;

import com.project.logistics.bean.*;
import com.project.logistics.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;

@Controller
public class RepairmanController {
    @Autowired
    EvaluateService evaluateService;
    @Autowired
    RepairsService repairsService;
    @Autowired
    UserService userService;

    @Autowired
    RepairmanService repairmanService;

    @Autowired
    UserDetailService userDetailService;

    @Autowired
    NoticeService noticeService;

    @RequestMapping("/repman/main")
    public String mainPage(Model model, HttpSession httpSession){
        List<Repairs> repairsList =repairsService.getAllRepairs();
        int all = repairsList.size();
        int undeal = repairsService.getAllUnRepairs().size();
        int deal = repairsService.getAllDealRepairs().size();
        Repairman todayRepairman = repairmanService.getTodayRepairman();
        List<Notice> notices = noticeService.getAllNotice();
        model.addAttribute("notices",notices);
        model.addAttribute("all",all);
        model.addAttribute("todayRepairman",todayRepairman);
        model.addAttribute("undeal",undeal);
        model.addAttribute("deal",deal);
        model.addAttribute("repairsList",repairsList);
        return "/repman/main";
    }

    //修改密码
    @RequestMapping("/repman/checkpwd")
    public String checkPassword(){
        return "/repman/password";
    }
    //提交修改密码
    @RequestMapping("/repman/updatepwd")
    public String updatePassword(String password, String newPassword, HttpSession httpSession, Model model){
        User user = (User)httpSession.getAttribute("user");
        if(user.getPassword().equals(password)){
            userService.updatePassword(newPassword,user.getUserId());
            return "/repman/main";
        }
        model.addAttribute("msg","密码输入错误");
        return "/repman/password";
    }
    //查看评价
    @RequestMapping("/repman/checkcom")
    public String checkComment(Model model,HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        List<Evaluate> evaluateList = evaluateService.getCommentsByRepairsmanId(user.getUserId());
        model.addAttribute("comments", evaluateList);
        return "/repman/comment";
    }

    @RequestMapping("/repman/checkcomdetail/{repairsId}")
    public String checkCommentDetail(Model model,@PathVariable int repairsId) {
        Repairs repairs = repairsService.getRepair(repairsId);
        model.addAttribute("repair", repairs);
        return "/repman/commentDetail";
    }

    //查看已处理
    @RequestMapping("/repman/checkrep/finish")
    public String checkRepairsFinish(Model model,HttpSession httpSession){
        Logger logger=Logger.getLogger(RepairmanController.class);
        logger.info("进入已处理");
        User user = (User)httpSession.getAttribute("user");
        System.out.println(user);
        List<Repairs> repairsList1 =repairsService.getRepairsByRepairsmanIdAndStatus(user.getUserId(),3);
        List<Repairs> repairsList2 =repairsService.getRepairsByRepairsmanIdAndStatus(user.getUserId(),4);
        repairsList1.removeAll(repairsList2);
        repairsList1.addAll(repairsList2);
        System.out.println(repairsList1);
        model.addAttribute("repairsList",repairsList1);
        return "/repman/repairsFinish";
    }

    //查看未处理
    @RequestMapping("/repman/checkrep/wait")
    public String checkRepairsWait(Model model,HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        List<Repairs> repairsList =repairsService.getRepairsByRepairsmanIdAndStatus(user.getUserId(),2);
        System.out.println(repairsList);
        model.addAttribute("repairsList",repairsList);
        return "/repman/repairs";
    }

    //修改报修状态
    @RequestMapping("/repman/checkrepdetail/{repairsId}")
    public String submitRepairs(Model model,@PathVariable int repairsId){
        Repairs repairs =(Repairs) repairsService.getRepair(repairsId);
        UserDetail user = userDetailService.getUserDetailById(repairs.getRepairsUserId());
        model.addAttribute("repair",repairs);
        model.addAttribute("user",user);
        return "/repman/repairsDetail";
    }

    //确认修改报修状态
    @RequestMapping("/repman/submitrep/{status}/{repairsId}")
    public String submitRepairs(@PathVariable int repairsId,@PathVariable int status){
        System.out.println(repairsId);
        System.out.println(status);
        repairsService.updateRepairsStatus(status,repairsId);
        return "/repman/main";
    }




    //跳转回复评价页面
    @RequestMapping("/repman/checkreply/{repairsId}")
    public String checkCommentReply(Model model,@PathVariable int repairsId){
        model.addAttribute("comment",evaluateService.getCommentById(repairsId));
        return "repman/reply";
    }

    //提交回复评价
    @RequestMapping("/repman/submitreply/{repairsId}")
    public String checkSubmitReply(@PathVariable int repairsId,String answer){
        evaluateService.updateEvaluateReply(new Evaluate(repairsId,new Date(),answer));
        return "repman/main";
    }
}
